using System;
using System.Collections.Generic;
using System.Text;
using BAFHTTPD;

namespace BAFHTTPDTest
{
    class Program
    {
        public int HomePage(RequestInfo Request, out string Page, out string ContentType)
        {
            ContentType = "text/html";
            Page =
                "<html><head><title>Welcome to BAFHTTPD!</title></head>\n" +
                "<body>\n" +
                "Welcome to BAFHTTPD! You are " + Request.EndPoint + ". This is just a simple test. Thanks!<br />\n" +
                "Be sure to visit the Test Page for the latest developments! The latest development: file support.<br />\n" +
                "<a href=\"/testpage.baf\">Test Page</a>\n" +
                "<a href=\"/headers.baf\">View Headers</a>\n" +
                "</body></html>";

            return 200;
        }

        public int TestPage(RequestInfo Request, out string Page, out string ContentType)
        {
            ContentType = "text/html";
            Page =
                "<html><head><title>Welcome to BAFHTTPD!</title></head>\n" +
                "<body>\n" +
                "Welcome to the testpage testpage.baf! File system file handling stuff (binding a virtual file to a real file) works! Example:<br />\n" +
                "<img src=\"/test.png\"><br>\n" +
                "<a href=\"/\">\"Home\" Page</a>\n" +
                "</body></html>";

            return 200;
        }

        public int HeadersPage(RequestInfo Request, out string Page, out string ContentType)
        {
            ContentType = "text/html";
            Page =
                "<html><head><title>Welcome to BAFHTTPD!</title></head>\n" +
                "<body>\n" +
                "Welcome to the Headers Page! Here are all the headers generated from your client:<br />\n" +
                "<pre>Method: " + Request.Method + "\n" +
                "Header Name = Value";

            foreach (KeyValuePair<string, string> Header in Request.Headers)
            {
                Page += "\n" + Header.Key + " = " + Header.Value;
            }

            Page += "\n\nGet Variables [" + Request.GetData.Count + "]";

            foreach (KeyValuePair<string, string> GetVar in Request.GetData)
            {
                Page += "\n" + GetVar.Key + " = " + GetVar.Value;
            }

            Page += "\n\nPost Variables [" + Request.PostData.Count + "]";

            foreach (KeyValuePair<string, string> PostVar in Request.PostData)
            {
                Page += "\n" + PostVar.Key + " = " + PostVar.Value;
            }

            Page +=
                "</pre><br>\n" +
                "Test Post: <form method=\"POST\" action=\"/headers.baf\"><input type=\"text\" name=\"testpostvar\"><input type=\"submit\" value=\"Test!\"></form><br>\n" +
                "<a href=\"/\">\"Home\" Page</a>\n" +
                "</body></html>";

            return 200;
        }

        public int Test403(RequestInfo Request, out string Page, out string ContentType)
        {
            //Page = ContentType = "";
            //return 403;
            BAFHTTPD.ErrorPageProvider ErrorPage = new ErrorPageProvider();
            return ErrorPage.Do403(Request, out Page, out ContentType);
        }

        Program()
        {
            BAFHTTPD.BAFHTTPD HTTPD = new BAFHTTPD.BAFHTTPD(31337);
            HTTPD.StartHttpD();
            HTTPD.Handlers.Add("/", new BAFHTTPD.BAFHTTPD.FileHandler(HomePage));
            HTTPD.Handlers.Add("/testpage.baf", new BAFHTTPD.BAFHTTPD.FileHandler(TestPage));
            HTTPD.Handlers.Add("/headers.baf", new BAFHTTPD.BAFHTTPD.FileHandler(HeadersPage));
            HTTPD.Handlers.Add("/403.baf", new BAFHTTPD.BAFHTTPD.FileHandler(Test403));
            HTTPD.Files.Add("/test.png", @"C:\Documents and Settings\Bobby\My Documents\My Pictures\dragon_ava.png");
            HTTPD.Directories.Add("/testdir/", @"C:\Documents and Settings\Bobby\My Documents\My Pictures\");
            HTTPD.Directories.Add("/dls/", @"C:\Documents and Settings\Bobby\Desktop\Downloads\2006\");

            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); // eeew a constructor that blocks until the program is killed.
        }

        static void Main(string[] args)
        {
            Program pg = new Program(); // ugly hack... this isn't even the way it's supposed to be done
        }
    }
}
